<?php

namespace app\admin\controller;

use think\Db;
use think\Request;
use think\Exception;//事务处理

class multilanguage extends Auth
{
    public function __construct(Request $request){
        $this->getChildIDdata = [];
        parent::__construct($request);
        $this->data = $request->param();
        $this->user_id = session('userid') == null ? 1 : session('userid');
        $this->lang = isset($this->data['lang']) && $this->data['lang'] ? $this->data['lang'] : is_lang();
    }

    public function list(Request $request)
    {   
        if($request->isAjax()){
            $where['lang'] = $this->lang;
            if(isset($this->data['searchText']) && $this->data['searchText']) $where['abbreviation'] = $this->data['searchText'];
            if(isset($this->data['searchContent']) && $this->data['searchContent']) $where['content'] = ['like','%'.$this->data['searchContent'].'%'];

            $list = getDatas('dict',['*'],$where);
            // 获取分页数据
            $returnData['size'] = count($list);
            if(isset($this->data['page']) && isset($this->data['limit'])){
                if($this->data['limit']*($this->data['page']-1) < $returnData['size']){
                    $list = array_slice($list,$this->data['limit']*($this->data['page']-1),$this->data['limit']);
                    $returnData['page'] = $this->data['page'];;
                }
            }else{
                $returnData['page'] =1;
            }
            $returnData['list'] = $list;
            $returnData['title']=lang('multiLanguage');
            // dump($returnData);die;
            return json_encode($returnData);

        }else{
            return view('list',['request'=>'list',['title'=>lang('multiLanguage')]]);
        }
        
    }

    public function addopen(){
        if(isset($this->data['request']) && $this->data['request'] == 'insert'){
            // return $this->data;
            if(!empty($this->data['val'])){
                // 开启事务
                Db::startTrans();   
                    $abbreviation = '';
                    foreach ($this->data['val'] as $k => $v) {
                        if(isset($v['content']) && $v['content']){
                            if(isset($v['lang']) && $v['lang']){
                                if(isset($v['abbreviation']) && $v['abbreviation']){
                                    $has = hasData('dict',['abbreviation'=>$v['abbreviation'],'lang'=>$v['lang']]);
                                    if($has){
                                        return json_encode(array('status'=>3,'msg'=>lang('abbreviation').'：'.$v['abbreviation'].' '.lang('already_exists')));
                                    }else{
                                        $has1 = hasData('dict',['content'=>$v['content'],'lang'=>$v['lang']]);
                                        if($has1){
                                            return json_encode(array('status'=>3,'msg'=>$v['lang'].' '.lang('translate_content').':'.$v['content'].' '.lang('already_exists')));
                                        }
                                        $res = insertData('dict',$v);
                                        $abbreviation .= $v['abbreviation'].',';
                                    }
                                }else{      
                                    return json_encode(array('status'=>2,'msg'=>lang('missing').' '.lang('abbreviation')));
                                }
                            }else{  
                                return json_encode(array('status'=>2,'msg'=>lang('missing').' '.lang('language')));
                            }
                        }else{  
                            return json_encode(array('status'=>2,'msg'=>lang('missing_name')));
                        }
                    }
                    rtrim($abbreviation,',');

                    if($res){
                        Db::commit();
                        return json_encode(array('status'=>1,'msg'=>lang('multiLanguage').lang('abbreviation').'->'.$abbreviation.lang('added_successfully')));
                    }else{
                        Db::rollback();
                        return json_encode(array('status'=>0,'msg'=>lang('multiLanguage').lang('abbreviation').'->'.$abbreviation.lang('add_failed')));
                    }
            }else{
                return json_encode(array('status'=>2,'msg'=>lang('improve_submission_data')));
            }
           
        }else{
            $language = getDatas('language',['*'],'1=1');
            // dump($language);die;
            return view('open',['title'=>lang("system_multilanguage_addition"),'type'=>'add','language'=>$language]);
        }
    }


    public function editopen(){
        if(isset($this->data['request']) && $this->data['request'] == 'update'){
            if(!empty($this->data['val'])){
                $abbreviation = '';
                foreach ($this->data['val'] as $k => $v) {
                    if(isset($v['content']) && $v['content']){
                        if(isset($v['lang']) && $v['content']){
                            if(isset($v['abbreviation']) && $v['content']){
                                $has_abbreviation = hasData('dict',['abbreviation'=>$v['abbreviation'],'id'=>['not in',$this->data['ids']],'lang'=>$v['lang']]);
                                $has_content = hasData('dict',['content'=>$v['content'],'id'=>['not in',$this->data['ids']],'lang'=>$v['lang']]);
                                if($has_abbreviation && $has_content){
                                    return json_encode(array('status'=>3,'msg'=>lang('abbreviation').'：'.$v['abbreviation'].' '.lang('already_exists')));
                                }
                                if($has_content){
                                    return json_encode(array('status'=>3,'msg'=>lang('abbreviation').'：'.$v['abbreviation'].' '.lang('content').'：'.$v['lang'].' '.lang('already_exists')));
                                }
                                $res = updateData('dict',['abbreviation'=>$v['abbreviation'],'lang'=>$v['lang']],$v);
                                $abbreviation .= $v['abbreviation'].',';
                                
                            }else{      
                                return json_encode(array('status'=>2,'msg'=>lang('missing').' '.lang('abbreviation')));
                            }
                        }else{  
                            return json_encode(array('status'=>2,'msg'=>lang('missing').' '.lang('language')));
                        }
                    }else{  
                        return json_encode(array('status'=>2,'msg'=>lang('missing_name')));
                    }
                    
                }
                rtrim($abbreviation,',');

                if($res>=0){
                    return json_encode(array('status'=>1,'msg'=>lang('multiLanguage').lang('abbreviation').'->'.$abbreviation.' '.lang('edited_successfully')));
                }else{
                    return json_encode(array('status'=>0,'msg'=>lang('multiLanguage').lang('abbreviation').'->'.$abbreviation.' '.lang('edit_failed')));
                }
            }else{
                return json_encode(array('status'=>0,'msg'=>lang('multiLanguage').lang('abbreviation').'->'.$abbreviation.' '.lang('edit_failed')));
            }
                    
        }else{
            $language = getDatas('language',['*'],'1=1');
            $content_id=[];
            foreach ($language as $k => $v) {
                $language[$k]['content'] = getValue('dict',['abbreviation'=>$this->data['abbreviation'],'lang'=>$v['code']],'content');
                $content_id[$k] = getValue('dict',['abbreviation'=>$this->data['abbreviation'],'lang'=>$v['code']],'id');
            }
            return view('open',['title'=>lang("system_multilanguage_editing"),'type'=>'edit','ids'=>implode(',', $content_id),'language'=>$language,'abbreviation'=>$this->data['abbreviation']]);
        }
    }

    public function changeStatus(){
        // return $this->data;
        if($this->data['id']){
            $res = updateData('language',['id'=>$this->data['id']],['hide'=>$this->data['status']]);
            if($res){
                return json_encode(array('status'=>1,'msg'=>lang('multiLanguage').' '.lang(getViewMultiLanguage($this->data['status'])).' '.lang('successfully')));
            }else{
                return json_encode(array('status'=>0,'msg'=>lang('multiLanguage').' '.lang(getViewMultiLanguage($this->data['status'])).' '.lang('failed')));
            }
        }else{
            return json_encode(array('status'=>2,'msg'=>'improve_submission_data'));
        }
    }

         // 清除
    public function requestDelete(){
        if($this->data['abbreviation']){
            $res = deleteData('dict',['abbreviation'=>$this->data['abbreviation']]);
            if($res){
                return json_encode(array('status' => 1,'msg'=>lang('multiLanguage').lang('abbreviation').'->'.$this->data['abbreviation'].' '.lang('cleared_successfully')));
            }else{
                return  json_encode(array('status' => 0,'msg'=>lang('multiLanguage').lang('abbreviation').'->'.$this->data['abbreviation'].' '.lang('clear_failed')));
            }
        }else{
            return json_encode(array('status' => 2,'msg'=>lang('improve_submission_data') ));
        }
    }


}